package com.xkdx.guangguang.fragment.shop.photo;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.shop.photo.GetPhotoCommentModule.PhotoComment;
import com.xkdx.guangguang.fragment.shop.photo.GetPhotoCommentModule.PhotoCommentItem;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.module.util.ErrorModule;
import com.xkdx.guangguang.shareclass.Photo;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class PhotoCommentFragment extends BaseFragment {
	View view;
	PhotoComment photoComment;
	int PageIndex = 1, PageSize = 100;
	String photoID;
	Photo photo;
	String creatTime;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	String picUrl, nickName, content, refreshTime, head, count;
	EditText comment_ed;
	Button back;
	ImageButton send;
	TextView name_tv, title_tv, time_tv;
	ImageView im, head_im;
	List<PhotoCommentItem> list;
	ListView listView;
	TextView count_tv;
	String userID, loginToken;
	GetPhotoCommentListAdapter adapter;
	// 提交评论
	PhotoCommentAction photoCommentAction;
	PhotoCommentModule photoCommentModule;
	PhotoCommentPresistence photoCommentPresistence;

	// 获取评论列表
	GetPhotoCommentAction getPhotoCommentAction;
	GetPhotoCommentModule getPhotoCommentModule;
	GetPhotoCommentPresistence getPhotoCommentPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		photoID = getArguments().getString("photoID");
		creatTime = getArguments().getString("creatTime");
		picUrl = getArguments().getString("picUrl");
		nickName = getArguments().getString("nickName");
		content = getArguments().getString("content");
		refreshTime = getArguments().getString("refreshTime");
		head = getArguments().getString("head");
		count = getArguments().getString("count");

		SharePrefenceUtil su = new SharePrefenceUtil(getActivity(),
				IConstants.SP_USER);
		userID = su.getUserID();
		loginToken = su.getUserLoginToken();

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo)
				.showImageOnFail(R.drawable.empty_photo)
				.resetViewBeforeLoading().cacheOnDisc()
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.photo_comment, container, false);
		findView();
		setOnClick();
		showLoading();
		return view;
	}

	@Override
	public void findView() {
		back = (Button) view.findViewById(R.id.photo_comment_back);
		im = (ImageView) view.findViewById(R.id.item_image);
		head_im = (ImageView) view.findViewById(R.id.head);
		imageLoader.displayImage(picUrl, im, options);
		imageLoader.displayImage(head, head_im, options);
		name_tv = (TextView) view.findViewById(R.id.name);
		title_tv = (TextView) view.findViewById(R.id.comment);
		time_tv = (TextView) view.findViewById(R.id.comment_date);
		count_tv = (TextView) view.findViewById(R.id.photo_comment_count);
		name_tv.setText(nickName);
		title_tv.setText(content);
		time_tv.setText(creatTime);
		comment_ed = (EditText) view.findViewById(R.id.photo_comment_content);
		send = (ImageButton) view.findViewById(R.id.photo_comment_send);
		listView = (ListView) view.findViewById(R.id.comment_list);

	}

	@Override
	public void setOnClick() {
		send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				subMitComment();
			}
		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getFragmentManager().popBackStack();
			}
		});

	}

	protected void subMitComment() {
		dataLoadDialog.show();
		IConstants.hideInputMethodAways(getActivity(), view);
		String cotent = comment_ed.getText().toString();
		photoCommentAction = new PhotoCommentAction(userID, loginToken,
				photoID, cotent);
		photoCommentModule = new PhotoCommentModule();
		photoCommentPresistence = new PhotoCommentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(photoCommentPresistence);
		photoCommentPresistence.setActitons(photoCommentAction);
		photoCommentPresistence.setModule(photoCommentModule);
		photoCommentPresistence.execute();

	}

	@Override
	public void showLoading() {
		getPhotoCommentAction = new GetPhotoCommentAction(photoID, loginToken);
		getPhotoCommentModule = new GetPhotoCommentModule();
		getPhotoCommentPresistence = new GetPhotoCommentPresistence(this);
		((ManagerActivity) getActivity())
				.addCurrentTask(getPhotoCommentPresistence);
		getPhotoCommentPresistence.setActitons(getPhotoCommentAction);
		getPhotoCommentPresistence.setModule(getPhotoCommentModule);
		getPhotoCommentPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (getPhotoCommentPresistence != null) {
					getPhotoCommentPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();
			}
		});

		dataLoadDialog.show();

	}

	// 获取
	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			setValuse();
		} else {
			refresh(map);
		}

	}

	private void setValuse() {
		photoComment = getPhotoCommentModule.photoComment;
		list = photoComment.getCommentlist();
		adapter = new GetPhotoCommentListAdapter(getActivity());
		listView.setAdapter(adapter);
		IConstants.setListViewHeightBasedOnChildren(listView);
		count_tv.setText(photoComment.getDataCount() + "条评论");
		IConstants.singlePhotoComment.clear();
		IConstants.singlePhotoComment.put(photoID, photoComment.getDataCount());
		comment_ed.setText("");
	}

	// 评论成功
	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			IConstants.isBrandPhotoComment = true;
			IConstants.isShopPhotoComment=true;
			Toast.makeText(getActivity(), "评论成功", Toast.LENGTH_SHORT).show();
			showLoading();
		} else {
			IConstants.isBrandPhotoComment = false;
			Toast.makeText(getActivity(),
					((ErrorModule) map.get("1")).errorMessage,
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

	class GetPhotoCommentListAdapter extends BaseAdapter {
		Context context;

		public GetPhotoCommentListAdapter(Context context) {
			super();
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list != null && list.size() != 0) {
				return list.size();
			} else {
				return 0;
			}

		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHodler viewHolder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.photo_comment_item, null);
				viewHolder = new ViewHodler();
				viewHolder.image = (ImageView) convertView
						.findViewById(R.id.photo_comment_item_im);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.photo_comment_item_name);
				viewHolder.content = (TextView) convertView
						.findViewById(R.id.photo_comment_item_content);
				viewHolder.time = (TextView) convertView
						.findViewById(R.id.photo_comment_item_time);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHodler) convertView.getTag();
			}
			imageLoader.displayImage(list.get(position).getUserAvatars(),
					viewHolder.image, options);
			viewHolder.name.setText(list.get(position).getNickName());
			viewHolder.content.setText(list.get(position).getCommentContent());
			viewHolder.time.setText(list.get(position).getCreateTime());
			return convertView;

		}

		class ViewHodler {
			TextView name, content, time;
			ImageView image;
		}

	}

}
